package com.richard.home.ui.fragment

import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ethanhua.skeleton.Skeleton
import com.ethanhua.skeleton.SkeletonScreen
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.richard.base.ext.toast
import com.richard.base.route.KsRouterHelper
import com.richard.base.route.RouterPath
import com.richard.base.ui.BaseVMListFragment
import com.richard.base.util.StatusBarUtil
import com.richard.home.R
import com.richard.home.model.bean.PoetryWord
import com.richard.home.ui.adapter.HomeRecPoetryAdapter
import com.richard.home.ui.header.RecHeaderLayout
import com.richard.home.viewModel.HomeRecViewModelImpl
import kotlinx.android.synthetic.main.home_rec_fragment.*
import org.koin.android.viewmodel.ext.android.getViewModel


@Route(path = RouterPath.Home.HOME_REC_PIC)
class HomeRecFragment : BaseVMListFragment<HomeRecViewModelImpl, PoetryWord>() {

    private var recPoetryAdapter : HomeRecPoetryAdapter = HomeRecPoetryAdapter()

    private var skeletonScreen: SkeletonScreen? = null

    override fun getLayoutResId(): Int = R.layout.home_rec_fragment

    init {
        recPoetryAdapter.setOnItemClickListener { adapter, view, position ->
            run {
                var recPoetry: PoetryWord = adapter.getItem(position) as PoetryWord
                KsRouterHelper.homePoetryDetail(recPoetry.poetryId!!)
            }
        }
    }

    override fun initView() {
        super.initView()
//        BusProvider.instance?.register(this)
        skeletonScreen = Skeleton.bind(clv_rec_images)
            .adapter(recPoetryAdapter)                        // your actual adapter
            .load(R.layout.home_shimmer_grid)       // skeleton item layout
            .build()
            .show()
        StatusBarUtil.setTranslucentForImageView(activity, 0, tv_title, true)
        clv_rec_images.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                val position = layoutManager.findFirstVisibleItemPosition()
                if (position > 0) {
                    rl_title.background.alpha = 255
                } else {
                    val firstView = layoutManager.findViewByPosition(position)
                    val top = firstView!!.top
                    val height: Int = context?.resources?.getDimension(R.dimen.base_sw_80dp)?.toInt()?:0
                    rl_title.background.alpha = Math.min(
                        255,
                        -top * 255 / height
                    )
                    Log.i("TAG","===alpha==" + rl_title.background.alpha)
                }
            }
        })
    }

    override fun initData() {
        mViewModel.getHomeRecPoetry(pageSize = getPageSize())
    }

    override fun wrapView(): View = clv_rec_images
    override fun getAdapter(): BaseQuickAdapter<PoetryWord, *> = recPoetryAdapter
    override fun getRecycleView(): RecyclerView = clv_rec_images
    override fun initVm(): HomeRecViewModelImpl = getViewModel()
    override fun getRefreshLayout(): TwinklingRefreshLayout = trl_content

    override fun startObserve() {
        mViewModel.apply {
            uiState.observe(this@HomeRecFragment, Observer { recModel ->
                recModel.showError?.let { errorMsg ->
                    context?.toast(errorMsg)
                }
                if (recPoetryAdapter.data.isNullOrEmpty()){
                    skeletonScreen?.hide()
                    context?.let {
                        recPoetryAdapter.addHeaderView(RecHeaderLayout(it))
                    }
                }
                recModel.courseInfo?.let {
                    setReqListSuccess(it)
                }
                // 结束下拉刷新
                getRefreshLayout().finishRefreshing()

            })
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() = HomeRecFragment()
    }

    override fun reqList(pageNo: Int, pageSize: Int) {
        mViewModel.getHomeRecPoetry(pageSize)
    }


}